/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
 * 
 * $Id: ContainerFrame.java 46 2008-05-15 18:09:39Z jens464 $
*/
package albareth.frontend.slick.ui;

import java.util.Iterator;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import albareth.core.entity.Item;
import albareth.core.entity.ItemContainer;
import albareth.core.entity.PlayerCharacter;
import albareth.frontend.slick.SlickFrontend;
import albareth.frontend.slick.SlickResources;
import albareth.frontend.slick.state.InGameState;

public class ContainerFrame extends BasicMenuFrame
{
    SlickFrontend _frontend;
    ItemContainer _container;
    
    
    public ContainerFrame(SlickFrontend frontend, ItemContainer container)
    {
        super(new ActionBarMenuView(frontend.getPlayerCharacter(), 32, 32, 8, 1));
        _container = container;
        _frontend = frontend;

        setId(InGameState.COMP_CONTAINER);
        setPageCounterVisible(false);
        setDraggingAllowed(true);
        setDroppingAllowed(true);
        setPadding(51, 5);
        setCurrentSelectedIndex(-1);
        setSelectionUpdatedOnMouseOver(true);
        
        rebuildInventory(container);
    }
    
    public ItemContainer getShownContainer()
    {
        return _container;
    }


    private void rebuildInventory(ItemContainer container)
    {
        removeAllItems();
        List inventory = container.getAllItems();
        for (Iterator it = inventory.iterator(); it.hasNext(); )
        {
            Item item = (Item)it.next();
            addItem(SlickFrontend.createMenuItemFromEntity(item));
        }
        
    }


    public void render(Graphics g, GameContainer container, int screenX,
            int screenY) throws SlickException
    {
        SlickResources.CONTAINER_BG.draw(screenX, screenY);
        SlickResources.getTileImage(_container.getTile()).draw(
                screenX-51, screenY, 2.0f);
        super.render(g, container, screenX, screenY);
    }

    public void itemContainerChanged(ItemContainer cont)
    {
        rebuildInventory(cont);
    }
    
    public DraggeableItem getDraggeableItem(int x, int y)
    {
        PlayerCharacter player = _frontend.getPlayerCharacter();
        
        DraggeableItem dragItem = super.getDraggeableItem(x, y);
        if (dragItem != null)
        {
            Item item = (Item)dragItem.getReferencedObject();
            if (!player.canPickUpItem(item)) {
                player.infoMsg("Cannot pick this up.");
                return null;
            }
        }
        return dragItem;
    }

    protected boolean tryDropItem(DraggeableItem droppedItem, int x, int y)
    {
        PlayerCharacter player = _frontend.getPlayerCharacter();

        if (!(droppedItem.getReferencedObject() instanceof Item)) {
            player.infoMsg("Only items can be dropped here.");
            return false;
        }
        Item item = (Item)droppedItem.getReferencedObject();
        
        if (!player.canDropItemIn(item, _container)) {
            player.infoMsg("Cannot drop it there.");
            return false;
        }
        
        return _container.addItem(item);
    }
    
    protected void mouseLeft(int newx, int newy)
    {
        setCurrentSelectedIndex(-1);
    }



}
